﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.MarketUI {
    import flash.events.*;
    import flash.display.*;
    import org.aswing.*;
    import Classes.*;
    import org.aswing.event.*;
    import Modules.stats.experiments.*;
    import org.aswing.ext.*;
    import Display.aswingui.*;

    public class MarketScrollingList extends JPanel {

        public static const SCROLL_VERTICAL:int = 0;
        public static const SCROLL_HORIZONTAL:int = 1;
        public static const NUM_ITEMS:int = 7;

        protected var m_dataList:GridList;
        protected var m_scrollPane:JScrollPane;
        protected var m_cellFactoryClass:Class;
        protected var m_cellFactory:GridListCellFactory;
        protected var m_viewport:JViewport;
        protected var m_frame:JWindow;
        protected var rightBtn:JButton;
        protected var leftBtn:JButton;
        protected var m_rows:int;
        protected var m_columns:int;
        protected var m_data:Array;
        protected var m_curCount:int = 0;
        protected var m_curIndex:int = 0;
        protected var m_model:VectorListModel;

        public function MarketScrollingList(_arg1:Array, _arg2:Class, _arg3:int, _arg4:int=0, _arg5:int=2){
            super(new SoftBoxLayout(SoftBoxLayout.X_AXIS, this.scrollingListGap, SoftBoxLayout.CENTER));
            this.m_columns = _arg4;
            this.m_rows = _arg5;
            this.m_data = _arg1;
            this.m_cellFactoryClass = _arg2;
            this.m_cellFactory = this.createCellFactory();
            this.makeData();
            this.makeButtons();
            this.initSize();
            this.prepare();
        }
        public function get numItems():int{
            return (NUM_ITEMS);
        }
        public function get curIndex():int{
            return (this.m_curIndex);
        }
        public function get currentPageIndex():int{
            return (((this.m_curIndex + (this.numItems - 1)) / this.numItems));
        }
        protected function get scrollingListGap():int{
            return (3);
        }
        protected function createCellFactory():GridListCellFactory{
            return (new this.m_cellFactoryClass());
        }
        protected function makeData():void{
            var _local1:int;
            var _local2:int = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_INSTANT_READY_SHOW);
            if (!_local2){
                _local1 = 0;
                while (_local1 < this.m_data.length) {
                    if ((((((this.m_data[_local1].localizedName == ZLoc.t("Items", "supply_all_business_friendlyName"))) || ((this.m_data[_local1].localizedName == ZLoc.t("Items", "mark_all_business_ready_friendlyName"))))) || ((this.m_data[_local1].localizedName == ZLoc.t("Items", "instant_ready_crop_friendlyName"))))){
                        this.m_data.splice(_local1, 1);
                        _local1--;
                    };
                    _local1++;
                };
            } else {
                _local1 = 0;
                while (_local1 < this.m_data.length) {
                    if (_local2 == 1){
                        if ((((this.m_data[_local1].localizedName == ZLoc.t("Items", "supply_all_business_friendlyName"))) || ((this.m_data[_local1].localizedName == ZLoc.t("Items", "mark_all_business_ready_friendlyName"))))){
                            this.m_data.splice(_local1, 1);
                            _local1--;
                        };
                    } else {
                        if (_local2 == 2){
                            if ((((this.m_data[_local1].localizedName == ZLoc.t("Items", "mark_all_business_ready_friendlyName"))) || ((this.m_data[_local1].localizedName == ZLoc.t("Items", "instant_ready_crop_friendlyName"))))){
                                this.m_data.splice(_local1, 1);
                                _local1--;
                            };
                        } else {
                            if ((((this.m_data[_local1].localizedName == ZLoc.t("Items", "supply_all_business_friendlyName"))) || ((this.m_data[_local1].localizedName == ZLoc.t("Items", "instant_ready_crop_friendlyName"))))){
                                this.m_data.splice(_local1, 1);
                                _local1--;
                            };
                        };
                    };
                    _local1++;
                };
            };
            var _local3:int = Math.min(this.numItems, this.m_data.length);
            this.m_model = new VectorListModel();
            var _local4:int;
            while (_local4 < _local3) {
                this.m_model.append(this.m_data[_local4]);
                this.m_curCount++;
                _local4++;
            };
            this.m_dataList = new GridList(this.m_model, this.m_cellFactory, this.m_columns, this.m_rows);
            this.m_scrollPane = new JScrollPane(this.m_dataList, JScrollPane.SCROLLBAR_NEVER, JScrollPane.SCROLLBAR_NEVER);
        }
        public function get data():Array{
            return (this.m_data);
        }
        public function get dataList():GridList{
            return (this.m_dataList);
        }
        protected function prepare():void{
            this.append(this.leftBtn);
            this.append(this.m_scrollPane);
            this.append(this.rightBtn);
            ASwingHelper.setEasyBorder(this, 0, 0, 0, 3);
            ASwingHelper.prepare(this);
        }
        protected function initSize():void{
            this.setPreferredHeight((this.scrollingListItemHeight * this.m_rows));
            this.m_scrollPane.setPreferredWidth(((this.scrollingListItemWidth - 1) * this.numItems));
            this.m_dataList.setHGap(7);
        }
        protected function getLeftInsets():Insets{
            return (new Insets(0, 0, 20, 3));
        }
        protected function getRightInsets():Insets{
            return (new Insets(0, 3, 20, 0));
        }
        protected function makeButtons():void{
            var _local1:DisplayObject = (new (Catalog.assetDict["btn_left_offstate"])() as DisplayObject);
            var _local2:DisplayObject = (new (Catalog.assetDict["btn_left_onstate"])() as DisplayObject);
            var _local3:DisplayObject = (new (Catalog.assetDict["btn_left_downstate"])() as DisplayObject);
            var _local4:DisplayObject = (new (Catalog.assetDict["btn_right_offstate"])() as DisplayObject);
            var _local5:DisplayObject = (new (Catalog.assetDict["btn_right_onstate"])() as DisplayObject);
            var _local6:DisplayObject = (new (Catalog.assetDict["btn_right_downstate"])() as DisplayObject);
            this.leftBtn = new JButton();
            this.leftBtn.wrapSimpleButton(new SimpleButton(_local1, _local2, _local3, _local1));
            this.leftBtn.setEnabled(false);
            var _local7:Insets = this.getLeftInsets();
            ASwingHelper.setEasyBorder(this.leftBtn, _local7.top, _local7.left, _local7.bottom, _local7.right);
            this.rightBtn = new JButton();
            this.rightBtn.wrapSimpleButton(new SimpleButton(_local4, _local5, _local6, _local4));
            if (this.m_data.length <= this.numItems){
                this.rightBtn.setEnabled(false);
            };
            var _local8:Insets = this.getRightInsets();
            ASwingHelper.setEasyBorder(this.rightBtn, _local8.top, _local8.left, _local8.bottom);
            this.rightBtn.addActionListener(this.moveRight, 0, true);
            this.leftBtn.addActionListener(this.moveLeft, 0, true);
        }
        public function removeListeners():void{
            var _local2:MarketCell;
            this.rightBtn.removeActionListener(this.moveRight);
            this.leftBtn.removeActionListener(this.moveLeft);
            var _local1:int;
            while (_local1 < this.m_curCount) {
                _local2 = (this.m_dataList.getCellByIndex(_local1).getCellComponent() as MarketCell);
                _local2.removeListeners();
                _local1++;
            };
        }
        public function updateElements():void{
            var _local2:MarketCell;
            var _local1:int;
            while (_local1 < this.m_curCount) {
                _local2 = (this.m_dataList.getCellByIndex(_local1).getCellComponent() as MarketCell);
                _local2.performUpdate();
                _local1++;
            };
        }
        public function scrollToItemIndex(_arg1:int):void{
            var _local2:int;
            var _local3:int;
            var _local4:int;
            var _local5:int;
            if (_arg1 < (this.m_curCount - this.numItems)){
                this.m_dataList.scrollHorizontal(0);
                this.m_dataList.scrollHorizontal((this.scrollingListItemWidth * _arg1));
            } else {
                if (_arg1 < this.m_curCount){
                    _local2 = this.m_curCount;
                    _local3 = Math.min(this.m_data.length, (_arg1 + this.numItems));
                    if ((_local3 - _local2) > 0){
                        if (this.m_model.getSize() < (this.numItems + _arg1)){
                            _local4 = _local2;
                            while (_local4 < _local3) {
                                this.m_model.append(this.m_data[_local4]);
                                this.m_curCount++;
                                this.m_curIndex++;
                                _local4++;
                            };
                        };
                    };
                    this.m_dataList.scrollHorizontal(0);
                    this.m_dataList.scrollHorizontal((this.scrollingListItemWidth * _arg1));
                } else {
                    _local2 = this.m_curCount;
                    if (_local2 != 0){
                        this.leftBtn.setEnabled(true);
                    };
                    _local3 = Math.min(this.m_data.length, (_arg1 + this.numItems));
                    if ((_local3 - _local2) > 0){
                        if (this.m_model.getSize() < (this.numItems + _arg1)){
                            _local5 = _local2;
                            while (_local5 < _local3) {
                                this.m_model.append(this.m_data[_local5]);
                                this.m_curCount++;
                                this.m_curIndex++;
                                _local5++;
                            };
                        };
                    };
                };
            };
            this.m_dataList.scrollHorizontal(0);
            this.m_dataList.scrollHorizontal((this.scrollingListItemWidth * _arg1));
        }
        protected function moveLeft(_arg1:AWEvent):void{
            this.m_curIndex = (this.m_curIndex - this.numItems);
            if (this.m_curIndex <= 0){
                this.leftBtn.setEnabled(false);
                this.m_curIndex = 0;
            };
            this.m_dataList.scrollHorizontal(-(this.scrollingListWidth));
            this.rightBtn.setEnabled(true);
            dispatchEvent(new Event(Event.CHANGE));
        }
        protected function moveRight(_arg1:AWEvent):void{
            var _local4:int;
            var _local2:int = this.m_curCount;
            var _local3:int = Math.min(this.m_data.length, (this.m_curCount + this.numItems));
            if ((_local3 - _local2) > 0){
                if (this.m_model.getSize() < (this.numItems + this.m_curCount)){
                    _local4 = _local2;
                    while (_local4 < _local3) {
                        this.m_model.append(this.m_data[_local4]);
                        this.m_curCount++;
                        this.m_curIndex++;
                        _local4++;
                    };
                    if (this.m_curIndex >= (this.m_data.length - this.numItems)){
                        this.rightBtn.setEnabled(false);
                    };
                };
            } else {
                if (this.m_curIndex < this.m_curCount){
                    this.m_curIndex = (this.m_curIndex + this.numItems);
                    if (this.m_curIndex >= (this.m_data.length - this.numItems)){
                        this.rightBtn.setEnabled(false);
                        this.m_curIndex = (this.m_data.length - this.numItems);
                    };
                };
            };
            this.leftBtn.setEnabled(true);
            this.m_dataList.scrollHorizontal(this.scrollingListWidth);
            dispatchEvent(new Event(Event.CHANGE));
        }
        public function get currentItem():int{
            var _local1:int = (this.m_curIndex - this.numItems);
            if (_local1 < 0){
                _local1 = 0;
            };
            return (_local1);
        }
        public function get currentItemName():String{
            var _local1:Item = this.m_data[this.currentItem];
            if (_local1 != null){
                return (_local1.name);
            };
            return (null);
        }
        protected function get scrollingListWidth():Number{
            return ((this.scrollingListItemWidth * this.numItems));
        }
        protected function get scrollingListItemWidth():Number{
            return (((Catalog.CARD_WIDTH + Catalog.CARD_OFFSET) + 5));
        }
        protected function get scrollingListItemHeight():Number{
            return (Catalog.CARD_HEIGHT);
        }

    }
}//package Display.MarketUI 
